/* -----------------------------------------------------
 iCompose
 Staff.m
 
 Created by Team: THE BEARD on 18/10/09.
 
 Members who contributed to this file:
 1. Bobby Luk
 2. Wilson
 3. Ethan Holmes
 
 List of changes to previous member:
 Version 1:
 Oct 18, 2009 (Bobby)
 - added class variables and methods as stated in the Design Document
 Oct 25, 2009 (Wilson)
 - implements addNote, removeNote, changeNote, and NavigateBar
 - Bar class needs more methods
 Oct 31, 2009 (Ethan)
 - implemented Encode / Decode methods
 Nov 2, 2009 (Ethan)
 - implemented getCurrentBeatAmount
 - implemented identfiyNoteButton
 - removed unimplemented methods
 
 Simon Fraser University, 2009.
 ----------------------------------------------------- */

#import "Staff.h"

static NSUInteger kNumberOfBars = 16;

@implementation Staff

@synthesize nameOfComposition;
@synthesize tempo;
@synthesize timeSig;
@synthesize currentBar;
@synthesize barList;
@synthesize barViews;

/*
 Initializes the staff according to the values the user inputed in OptionView.
 A bar is generated for the user to start adding notes.
 */
-(void)initStaff:(TimeSignature*)time andTempo:(Tempo*)speed andInstrument:(InstrumentPlayer*) player andName:(NSString*)name {
	NSMutableArray *bars = [[NSMutableArray alloc] init];
    for (unsigned i = 0; i < kNumberOfBars; i++) {
        [bars addObject:[NSNull null]];
    }
    self.barList = bars;
    [bars release];
	
	barViews = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 44, 480, 232)];
	
	// a page is the width of the scroll view
    barViews.pagingEnabled = YES;
	
    barViews.contentSize = CGSizeMake(barViews.frame.size.width * kNumberOfBars, barViews.frame.size.height);
	barViews.backgroundColor = [UIColor clearColor];
    barViews.showsHorizontalScrollIndicator = NO;
    barViews.showsVerticalScrollIndicator = NO;
    barViews.scrollsToTop = NO;
	
	nameOfComposition = name;
	timeSig = time;
	tempo = speed;
	instrumentPlayer = player;
	
	currentBar = 0;
	
	[self loadBarViewsWithBar:0];
}

/*
 The InstrumentPlayer is given an NSMutableArray (acting as a Queue) containing all the notes in the current program.
 If there is a note that is not placed on the staff an error message is displayed.
 Using the NSMutableArray the InstrumentPlayer plays the corresponding notes.
 */
-(void)play {
	NSMutableArray *playList = [[NSMutableArray alloc] initWithCapacity: 0];
	for(Bar *bar in barList) {
		if (!((NSNull *)bar == [NSNull null])) {
			for(NoteNode *node in bar.currentNotes) {
				[playList addObject: node];
			}
		}
	}
	[instrumentPlayer playNotes: playList andTempo: tempo];
	[playList release];
}

/*
 Stop playing all notes
 */
-(void)stop {
	[instrumentPlayer stopNotes];
}

/*
 Clears all rest graphics
 */
- (void)clearStaff {
	for (Bar *bar in barList) {
		if (!((NSNull *)bar == [NSNull null])) {
			[bar clearBar];
		}
	}
}

/*
 Lazily loads bars as needed
*/
- (void)loadBarViewsWithBar:(int)barNumber {
    if (barNumber < 0) return;
    if (barNumber >= kNumberOfBars) return;
	
    // replace the placeholder if necessary
    Bar *newBar = [barList objectAtIndex:barNumber];
    if ((NSNull *)newBar == [NSNull null]) {
        newBar = [[Bar alloc] init];
        [barList replaceObjectAtIndex:barNumber withObject:newBar];
        [newBar release];
    }
	
    // add the bars's view to the scroll view
    if (nil == newBar.barView.superview) {
        CGRect frame = barViews.frame;
        frame.origin.x = frame.size.width * barNumber;
        frame.origin.y = 0;
        newBar.barView.frame = frame;
		[barViews addSubview:newBar.barView];
    }
}


-(id)initWithCoder:(NSCoder*)coder {
	nameOfComposition = [[coder decodeObjectForKey:@"nameOfComposition"] retain];
	timeSig = [[coder decodeObjectForKey:@"timeSig"] retain];
	tempo = [[coder decodeObjectForKey:@"tempo"] retain];
	currentBar = 0;
	barList = [[coder decodeObjectForKey:@"barList"] retain];
	instrumentPlayer = [[InstrumentPlayer alloc] init];
	barViews = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 44, 480, 232)];
	
	// a page is the width of the scroll view
    barViews.pagingEnabled = YES;
	
    barViews.contentSize = CGSizeMake(barViews.frame.size.width * kNumberOfBars, barViews.frame.size.height);
	barViews.backgroundColor = [UIColor clearColor];
    barViews.showsHorizontalScrollIndicator = NO;
    barViews.showsVerticalScrollIndicator = NO;
    barViews.scrollsToTop = NO;
	
	return self;
}

-(void)encodeWithCoder:(NSCoder*)encoder {
	[encoder encodeObject:nameOfComposition forKey:@"nameOfComposition"];
	[encoder encodeObject:timeSig forKey:@"timeSig"];
	[encoder encodeObject:tempo forKey:@"tempo"];
	[encoder encodeObject:barList forKey:@"barList"];
}


@end
